<?php
/**
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/10/10
 * Time: 10:59
 */

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Files extends Controller
{

    public $_model;

    public function _initialize()
    {
        $this->_model = new \app\admin\model\Files();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    /**
     * 文件批量上传使用的函数
     */
    public function fileuploads()
    {
        $rdata = [
            'err' => 1,
            'msg' => "",
            'thumb1' => "",
            'thumb2' => "",
            'thumb3' => "",
            'url' => "",
            'id' => 0
        ];
        $res = uploadImage("file");
        $time = time();
        $types = 0;

        if ($res['err'] == 0) {
            $data = array(
                'thumb1' => $res['thumb1'],
                'thumb2' => $res['thumb2'],
                'thumb3' => $res['thumb3'],
                'url' => $res['url'],
                'ctime' => $time,
                'types' => $types,
            );
            $result = $this->_model->addData($data);
            if ($result['err'] == 0) {
                $rdata['err'] = 0;
                $rdata['msg'] = "上次成功";
                $rdata['thumb1'] = $res['thumb1'];
                $rdata['thumb2'] = $res['thumb2'];
                $rdata['thumb3'] = $res['thumb3'];
                $rdata['url'] = $res['url'];
                $rdata['id'] = $result['insert_id'];
            }
        }
        return json($rdata);
    }

    public function lists()
    {

        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_ty = trim(input("request.ser_ty"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }

        if (!empty($ser_ty)) {
            $where['ty'] = $ser_ty;
        }

        $starttime = trim(input("request.starttime"));//auth
        $endtime = trim(input("request.endtime"));//auth


        if (!empty($starttime) && !empty($endtime)) {
            $where['ctime'] = array('between', strtotime($starttime . " 00:00:00") . "," . strtotime($endtime . " 23:59:59"));
        }
        //查询
        $filesModel = new \app\admin\model\Files();


        $lists = $filesModel->lists($where, $this->pagesize);

        $this->assign('lists', $lists);

        $this->assign('request', input("request."));
        return view('lists');

    }

    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $oldFile = $this->_model->getID($id);

        $res = $this->_model->delData(array('id' => $id));

        if ($res) {

            removeFileForDirectory($oldFile['file']);
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }


    }


}